package com.example.test.student;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import com.example.test.R;
import com.example.test.teacher.SetChen;
import com.example.test.utils.MyApplication;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

public class MainActivity extends AppCompatActivity {

    private TextView mTv_7;
    private GridView gridView;
    private ListView listView;
    private List<Map<String, Object>> dataList;
    private List<Map<String, Object>> listView_data;
    private int[] icon= {R.drawable.lesson,R.drawable.grade,R.drawable.tongzhi,R.drawable.my};
    private String[] iconName={"课程表","成绩查询","通知","个人信息"};
    private String[] title = {"重庆电子工程职业学院关于2021年元旦节放假的通知","关于“专本贯通3+2”项目2021届 专科生(重庆文理学院)","重庆电子工程职业学院2020年度公开招聘事业单位工作人"};
    private int[][] pic = {{R.drawable.pic1,R.drawable.pic2,R.drawable.pic3},{R.drawable.pic4,R.drawable.pic5,R.drawable.pic6},{R.drawable.pic7,R.drawable.pic8,R.drawable.pic9}};
    private Class[] gridViewActivity = {LessonActivity.class, GradeActivity.class, Message.class, UserInfo.class};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_student);
        init();
        setGridView();
        setListView();
        setListViewHeightBasedOnChildren(listView);
        gridViewClick();
        banner();
    }

    private void banner() {
        final int[] imgs = {R.drawable.mainimg,R.drawable.pic1,R.drawable.pic2,R.drawable.pic3,R.drawable.pic4};
        new Thread(new Runnable() {
            @Override
            public void run() {
                int i = 0;
                while (true){
                    if(i<imgs.length - 1){
                        i++;
                    } else {
                        i=0;
                    }
                    final int finalI = i;
                    runOnUiThread(new Runnable() {
                        ImageView img = findViewById(R.id.imageView6);
                        @Override
                        public void run() {
                            img.setImageResource(imgs[finalI]);
                        }
                    });
                    try {
                        Thread.sleep(5000);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                }
            }
        }).start();
    }

    public void init()
    {
        mTv_7 = findViewById(R.id.textView4);
        mTv_7.setSelected(true);
        gridView = findViewById(R.id.gridView);
        listView = findViewById(R.id.listView);
        if (MyApplication.getGlobalvariable().equals("teacher")){
            this.iconName = new String[]{"创建课程", "成绩发布", "通知", "个人信息"};
            this.gridViewActivity = new Class[]{EditLesson.class, SetChen.class, Message.class, UserInfo.class};
        }

    }

    public void setGridView()
    {
        dataList=new ArrayList<>();
        for(int i=0;i<icon.length;i++)
        {
            Map<String, Object> map= new HashMap<>();
            map.put("pic", icon[i]);
            map.put("text", iconName[i]);
            dataList.add(map);
        }
        SimpleAdapter adapter=new SimpleAdapter(this, dataList, R.layout.student_gridview, new String[]{"pic","text"}, new int[]{R.id.imageView,R.id.textView});
        gridView.setAdapter(adapter);
    }

    public void setListView()
    {
        listView_data=new ArrayList<>();
        for(int i=0;i<title.length;i++)
        {
            Map<String, Object> map= new HashMap<>();
            map.put("title", title[i]);
            map.put("pic1",pic[i][0]);
            map.put("pic2",pic[i][1]);
            map.put("pic3",pic[i][2]);
            listView_data.add(map);
        }
        SimpleAdapter adapter=new SimpleAdapter(this, listView_data, R.layout.student_listview_main, new String[]{"pic1","title","pic2","pic3"}, new int[]{R.id.imageView2,R.id.textView2,R.id.imageView3,R.id.imageView4});
        listView.setAdapter(adapter);
    }

    public void setListViewHeightBasedOnChildren(ListView listView) {
        ListAdapter listAdapter = listView.getAdapter();
        if (listAdapter == null) {
            return;
        }

        int totalHeight = 0;
        for (int i = 0; i < listAdapter.getCount(); i++) {
            View listItem = listAdapter.getView(i, null, listView);
            listItem.measure(0, 0);  // 获取item高度
            totalHeight += listItem.getMeasuredHeight();
        }

        ViewGroup.LayoutParams params = listView.getLayoutParams();
        // 最后再加上分割线的高度和padding高度，否则显示不完整。
        params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1))+listView.getPaddingTop()+listView.getPaddingBottom();
        listView.setLayoutParams(params);
    }

    public void gridViewClick()
    {
        gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent = new Intent(MainActivity.this,gridViewActivity[position]);
                startActivity(intent);
            }
        });
    }
}
